package com.vchello.controller;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.vchello.bean.feedback.Feedback;
import com.vchello.bean.user.User;
import com.vchello.service.feekback.FeedbackService;
import com.vchello.util.ToolUtil;
import com.vchello.util.UploadFileHelper;
import com.vchello.util.Utility;

/**
 * 
 * @author power
 *
 */
@Controller
@RequestMapping(value = "/feedback")
public class FeedbackController extends BaseController {
	
	@Autowired
	private FeedbackService feedbackService;
	
	@RequestMapping(value = "/saveFeedback", method = RequestMethod.GET)
	public String saveFeedback(Model model){
		model.addAttribute("feedback", new Feedback());
		return "feedback";
		
	}
	
	@RequestMapping(value = "/saveFeedback", method = RequestMethod.POST)
	public void saveFeedback(Feedback feedback, String picValue) throws IOException{
		User user = getUser();
		if(null == user){
			error("用户未登录");
			return;
		}
		String[] picArray = picValue.split(",");
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < picArray.length; i++) {
			String logoStr = UploadFileHelper.isFileExists(picArray[i], request);
			sb.append(logoStr).append(";");
		}
		feedback.setContent(ToolUtil.getFixStr(feedback.getContent()));
		feedback.setScreenshot(sb.toString());
		feedback.setUserName(user.getUserName());
		feedback.setEmail(user.getEmail());
		feedback.setPhone(user.getPhone());
		feedback.setCreateTime(Utility.getSimpleFormatedDayNow());
		boolean isTrue = feedbackService.saveFeedback(feedback)>0;
		if(isTrue) {
			success("提交反馈成功");
			return;
		}
		error("提交反馈失败");
	}
}
